/* =============================================================================
 * evolution of the cross-sectional golden ages
 * ===========================================================================*/

 
/*******************************************************************************
************************************ China *************************************
*******************************************************************************/

use "data\China.dta", clear

drop if outlier

drop if age < 20
drop if age > 64

keep if male == 1

* nonparametric predicted value at grids
gen x = _n/2 + 25 in 1/70
forvalues t = 1986/2012 {
	lpoly lnearn age if year == `t', nograph gen(pred`t') at(x)
}
keep x pred*
drop if x == .
reshape long pred, i(x) j(year)

* find golden ages
bys year: egen max_pred = max( pred )
gen distance = abs( pred - max_pred )
keep if distance < 10^(-6)
keep year x
rename x golden_age_china
save "temp\golden_age_cn.dta", replace



/*******************************************************************************
************************************* US ***************************************
*******************************************************************************/

use "data\US.dta", clear

drop if outlier

drop if age < 20
drop if age > 64

keep if male == 1

* nonparametric predicted value at grids
gen x = _n/2 + 25 in 1/70
forvalues t = 1986/2012 {
	lpoly lnearn age if year == `t', nograph gen(pred`t') at(x)
}
keep x pred*
drop if x == .
reshape long pred, i(x) j(year)

* find golden ages
bys year: egen max_pred = max( pred )
gen distance = abs( pred - max_pred )
keep if distance < 10^(-6)
keep year x
rename x golden_age_us
save "temp\golden_age_us.dta", replace



/*******************************************************************************
****************************** fit linear trend ********************************
*******************************************************************************/

use "temp\golden_age_us.dta", clear
merge 1:1 year using "temp\golden_age_cn.dta"

twoway (lfit golden_age_china year, clstyle(p1line) clpattern(dash_dot) clcolor(red*0.5))	///
	(lfit golden_age_us year, clstyle(p1line) clpattern(shortdash) clcolor(blue*0.5))	///
	(scatter golden_age_china year, msymbol(oh) mcolor(red) msize(large))		///
	(scatter golden_age_us year, msymbol(x) mcolor(blue) msize(large)),	 	///
	xlabel(, labsize(medlarge)) ylabel(30(5)60, labsize(medlarge))		///
	ytitle("Golden Age", size(medlarge))  xtitle("Year", size(medlarge))		///
	legend(order(3 "China" 4 "U.S.") pos(4) size(medlarge))		
	
graph export "figures\evolution_golden_age.pdf", as(pdf) replace	
